package com.tiger.web.controller.communal;

import java.util.List;

import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.communal.domain.CommonAgency;
import com.tiger.communal.service.ICommonAgencyService;
import com.tiger.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tiger.common.annotation.Log;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.enums.BusinessType;
import com.tiger.common.core.page.TableDataInfo;

/**
 * 代营公司Controller
 *
 * @author tiger
 * @date 2022-09-13
 */
@RestController
@RequestMapping("/system/agency")
@Api(tags = "代营业公司管理")
public class CommonAgencyController extends BaseController {
    @Autowired
    private ICommonAgencyService commonAgencyService;

    /**
     * 查询代营公司列表
     */
//    @PreAuthorize("@ss.hasPermi('system:agency:list')")
    @ApiOperation(value = "查询代营业公司管理列表")
    @GetMapping("/list")
    public TableDataInfo list(CommonAgency commonAgency) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        commonAgency.setCompanyId(loginUser.getUser().getDeptId());
        startPage();
        List<CommonAgency> list = commonAgencyService.selectCommonAgencyList(commonAgency);
        return getDataTable(list);
    }


    /**
     * 获取代营公司详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:agency:query')")
    @ApiOperation(value = "查询代营业公司详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(commonAgencyService.selectCommonAgencyById(id));
    }

    /**
     * 新增代营公司
     */
//    @PreAuthorize("@ss.hasPermi('system:agency:add')")
//    @Log(title = "代营公司", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增代营业公司")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CommonAgency commonAgency) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        commonAgency.setCompanyId(loginUser.getUser().getDeptId());
        return toAjax(commonAgencyService.insertCommonAgency(commonAgency));
    }

    /**
     * 修改代营公司
     */
//    @PreAuthorize("@ss.hasPermi('system:agency:edit')")
//    @Log(title = "代营公司", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiOperation(value = "修改代营业公司")
    public AjaxResult edit(@RequestBody CommonAgency commonAgency) {
        return toAjax(commonAgencyService.updateCommonAgency(commonAgency));
    }

    /**
     * 删除代营公司
     */
//    @PreAuthorize("@ss.hasPermi('system:agency:remove')")
//    @Log(title = "代营公司", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除代营业公司")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(commonAgencyService.deleteCommonAgencyByIds(ids));
    }
}
